package com.hw.handler;

import com.hw.common.ResponseCode;
import com.hw.model.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author whw
 * @date 2020/4/1 9:27
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理Exception
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult<?> exceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseResult.error(e.getMessage());
    }

    /**
     * 处理UnauthorizedException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResponseResult<?> unauthorizedExceptionHandler(UnauthorizedException e) {
        log.error(e.getMessage(), e);
        return ResponseResult.error(ResponseCode.NOT_PERMISSION.getCode(), ResponseCode.NOT_PERMISSION.getMsg());
    }
}
